using UnityEngine;
using System.Collections;

public class OrbitPlanet_New : MonoBehaviour {

    public float _speed = 6.0f;
    private float _zScale;
    private float _lastZScale;
    public Transform _planetCore;

    private Vector3 _gravitationalPull = Vector3.zero;
    private Vector3 _moveDirection = Vector3.zero;
    private Vector3 _targetPosition = Vector3.zero;
    private Vector3 _currentNormal;

    bool _isFalling = true;

    // Use this for initialization
    void Start()
    {
        _zScale = _lastZScale = transform.localScale.z;
        _currentNormal = transform.up;
        //gameObject.AddComponent("Rigidbody");
        //gameObject.rigidbody.useGravity = false
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        CharacterController characterController = (CharacterController)GetComponent("CharacterController");

        // Vector3 targetDir = transform.TransformDirection(-Vector3.up);
        Vector3 objToPlanetDirection = (_planetCore.transform.position - transform.position).normalized;

        RaycastHit raycastHit;
        if (!Physics.Raycast(transform.position, objToPlanetDirection, out raycastHit, 1.5f))
        {
            // _gravitationalPull += (transform.position - _planetCore.position).normalized * -2 * Time.deltaTime;
            _gravitationalPull = (objToPlanetDirection.normalized / (objToPlanetDirection.magnitude * objToPlanetDirection.magnitude)) * _planetCore.rigidbody.mass * 1.0f;
          //  characterController.Move(_gravitationalPull * 3.0f * Time.deltaTime);
        }
        else
        {
            _gravitationalPull = Vector3.zero;
        }

        _moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
        _moveDirection = transform.TransformDirection(_moveDirection);
        _moveDirection *= _speed;

        float inputDirection = Input.GetAxis("Horizontal");

        if (inputDirection > 0.0f)
        {
            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, _zScale);
            _lastZScale = _zScale;
        }
        else if (inputDirection < 0.0f)
        {
            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, -_zScale);
            _lastZScale = -_zScale;
        }
        else if (inputDirection == 0.0f)
        {
            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, _lastZScale);
        }

        float damping = 1.0f;
        //_currentNormal.x = Mathf.Lerp(_currentNormal.x, raycastHit.normal.x, damping);
        //_currentNormal.y = Mathf.Lerp(_currentNormal.y, raycastHit.normal.y, damping);
        //_currentNormal.z = Mathf.Lerp(_currentNormal.z, raycastHit.normal.z, damping); 

        //Quaternion quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
        //transform.rotation = quaternionRotation * transform.rotation;

        Vector3 finalMovement = new Vector3();
        _gravitationalPull *= 9.81f;
        finalMovement.x = _moveDirection.x * _gravitationalPull.x;
        finalMovement.x = _moveDirection.y * _gravitationalPull.y;
        finalMovement.x = _moveDirection.z * _gravitationalPull.z;

        //characterController.Move(_moveDirection * Time.deltaTime);
       characterController.Move(finalMovement * Time.deltaTime);
    }


    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        //print("Hit!!!");
        _gravitationalPull = Vector3.zero;
        _currentNormal = Vector3.Lerp(_currentNormal, hit.normal, 0.3f);
        Quaternion quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
        transform.rotation = quaternionRotation * transform.rotation;
    }

    void OnCollisionExit(Collision collision)
    {
        print("Exit");
    }
}
